package cn.doitedu.day04

import org.apache.spark.{SparkConf, SparkContext}

object T05_CountDemo {

  def main(args: Array[String]): Unit = {

    //1.创建SparkConf
    val conf = new SparkConf().setAppName("WordCount")
      .setMaster("local[4]") //如果提交到集群中运行，setMaster必须注释掉

    //2.创建SparkContext
    val sc = new SparkContext(conf)

    val arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    val rdd1 = sc.parallelize(arr, 4)

    //val count: Long = rdd1.count()
    val result: Array[Int] = sc.runJob(rdd1, (it: Iterator[Int]) => {
      var count = 0
      while (it.hasNext) {
        count +=1
        it.next()
      }
      count
    })

    val count = result.sum
    println(count)
  }

}
